<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="../../bower_components/paper-input/paper-input.html">
<link rel="import" href="../../bower_components/paper-input/paper-input-error.html">
<link rel="import" href="../../bower_components/paper-input/paper-textarea.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="../../bower_components/mist-list/mist-list-actions.html">
<link rel="import" href="../../bower_components/mist-list/mist-list-actions-behavior.html">
<link rel="import" href="../helpers/transfer-ownership.html">
<link rel="import" href="../tags/tags-form.html">

<dom-module id="network-actions">
  <template>
    <style include="shared-styles">
      mist-list-actions {
        width: 100%;
      }
    </style>

    <dialog-element id="confirm"></dialog-element>
    <tags-form id="tagsdialog" model="[[model]]" items="[[items]]" type="[[type]]"></tags-form>
    <transfer-ownership id="ownershipdialog" user="[[user]]" members="[[_otherMembers(members,items.length)]]" items="[[items]]" type="[[type]]"></transfer-ownership>

    <iron-ajax id="request" handle-as="json" loading="{{loadingData}}" on-response="handleResponse" on-error="handleError"></iron-ajax>

    <slot>
        <mist-list-actions actions=[[actions]]></mist-list-actions>
    </slot>

  </template>
  <script>
    NETWORK_ACTIONS = {
      'tag': {
        'name': 'tag',
        'icon': 'label',
        'confirm': true,
        'multi': true
      },
      'transfer-ownership': {
        'name': 'transfer ownership',
        'icon': 'icons:redo',
        'confirm': false,
        'multi': true
      },
      'delete': {
        'name': 'delete',
        'icon': 'delete',
        'confirm': true,
        'multi': true
      }
    }
    Polymer({
      is: 'network-actions',
      properties: {
        items: {
          type: Array,
          value: []
        },
        actions: {
          type: Array,
          value: [],
          notify: true
        },
        type: {
          type: String,
          value: 'network'
        },
        members: {
            type: Array
        },
        user: {
            type: String
        },
        org: {
            type: Object
        }
      },
      observers: [
        '_mapPolicyToActions(items.*,org,user)'
      ],
      listeners: {
        'select-action': 'selectAction',
        'confirmation': 'confirmAction',
        'transfer-ownership': 'transferOwnership'
      },
      ready: function () {},
      attached: function () {
        this.$.request.headers["Content-Type"] = 'application/json';
        this.$.request.headers["Csrf-Token"] = CSRF_TOKEN;
        this.$.request.method = "POST";
      },
      itemActions: function (network) {
        var arr = [];
        if (network) {
          arr.push('delete');
          arr.push('tag');
          if (this.org.ownership_enabled && (network.owned_by == this.user || this.org.is_owner)) {
            arr.push('transfer-ownership');
          }
        }
        return arr;
      },
      actionDetails: function (actions) {
        var ret = [];
        for (var i = 0; i < actions.length; i++) {
          ret.push(NETWORK_ACTIONS[actions[i]]);
        }
        return ret;
      },
      _otherMembers: function (members,items) {
        if (this.items && members) {
          var owners = this.items.map(function(i){return i.owned_by;})
                            .filter(function(value,index,self){return self.indexOf(value) === index;});
          // filter out pending users and the single owner of the item-set if that is the case
          return members.filter(function(m) {
              return owners.length == 1 ? m.id != owners[0] && !m.pending : !m.pending;
          });
        }
      },
      _delete: function () {
        //set up iron ajax
        this.$.request.headers["Content-Type"] = 'application/json';
        this.$.request.headers["Csrf-Token"] = CSRF_TOKEN;
        this.$.request.method = "DELETE";
        this.$.request.body = null;

        for (var i = 0; i < this.items.length; i++) {
          this.$.request.url = "/api/v1/clouds/" + this.items[i].cloud + "/networks/" + this.items[i].id
          this.$.request.generateRequest();
          this.dispatchEvent(new CustomEvent('toast', { bubbles: true, composed: true, detail:  {
            msg: 'Deleting ' + this.items[i].name,
            duration: 1000
          } }))
        }
      },
      _showDialog: function (info) {
        var dialog = this.shadowRoot.querySelector('dialog-element');
        for (var i in info) {
          dialog[i] = info[i];
        }
        dialog._openDialog();
      },
      confirmAction: function (e) {
        if (e.detail.confirmed)
          this.performAction(this.action, this.items);
      },
      selectAction: function (e) {
        if (this.items.length) {
          var action = e.detail.action;
          this.set('action', action);
          // console.log('perform action mist-action', this.items);
          if (action.confirm && action.name != 'tag') {
            var property = ['zone'].indexOf(this.type) == -1 ? "name" : "domain",
              plural = this.items.length == 1 ? '' : 's',
              count = this.items.length > 1 ? this.items.length + ' ' : '';
            //this.tense(this.action.name) + " " + this.type + "s can not be undone. 
            this._showDialog({
              title: this.action.name + ' ' + count + this.type + plural + '?',
              body: "You are about to " + this.action.name + " " + this.items.length + " " + this.type +
                plural + ".",
              list: this._makeList(this.items, property),
              action: action.name,
              danger: true,
              reason: this.type + "." + this.action.name
            });
          } else if (action.name == 'transfer ownership') {
            this.$.ownershipdialog._openDialog();
          } else if (action.name == "tag") {
            this.$.tagsdialog._openDialog();
          } else {
            this.performAction(this.action, this.items);
          }
        }
      },
      transferOwnership: function (e) {
        var payload = {
          user_id: e.detail.user_id, //new owner
          resources: {}
        };
        payload.resources[this.type] = this.items.map(function(i){return i.id});
        console.log('transferOwnership', e.detail, payload);
        this.$.request.url = '/api/v1/ownership';
        this.$.request.headers["Content-Type"] = 'application/json';
        this.$.request.headers["Csrf-Token"] = CSRF_TOKEN;
        this.$.request.method = "POST";
        this.$.request.body = payload;
        this.$.request.generateRequest();
      },
      performAction: function (action, items) {
        if (action.name == 'delete') {
          this._delete();
        }
      },
      handleResponse: function (e) {
        this.dispatchEvent(new CustomEvent('action-finished'));



        // console.log('handleResponse',e);
        if (this.$.request && this.$.request.body && this.$.request.body.action)
          this.dispatchEvent(new CustomEvent('toast', { bubbles: true, composed: true, detail:  {
            msg: 'Action: ' + this.$.request.body.action + ' successfull',
            duration: 3000
          } }))
        if (e.detail.xhr.responseURL.endsWith("api/v1/ownership") && e.detail.xhr.status == 200 ) {
          this.$.ownershipdialog._closeDialog();
          this.dispatchEvent(new CustomEvent('toast', { bubbles: true, composed: true, detail: {
            msg: 'Successfull ownership transfer',
            duration: 3000
          } }));

        }
        if (!this.$.request.body || !this.$.request.body.action && this.$.request.method == "DELETE") {
          this.dispatchEvent(new CustomEvent('network-deleted', { bubbles: true, composed: true, detail: {responseURL: e.detail.xhr.responseURL} }));

        }
      },
      _mapPolicyToActions: function (items) {
        // recompute the actions array property as the intersection
        // of the available actions of the selected items
        this.set('actions', []);
        var actions = new swiftSet.Set(),
          isection = new swiftSet.Set();

        if (this.items.length > 0) {
          actions.addItems(this.itemActions(this.items[0]) || []);

          for (var i = 1; i < this.items.length; i++) {
            isection.clear()
            isection.addItems(actions.intersection(this.itemActions(this.items[i])));
            actions.clear();
            actions.addItems(isection.items());
          }

          var multiActions;

          if (this.items.length > 1) {
            multiActions = this.actionDetails(actions.items()).filter(function (a) {
              return a.multi;
            });
          } else {
            multiActions = this.actionDetails(actions.items());
          }
        }
        this.set('actions', multiActions);
      },
      handleError: function (e) {
        // console.log(e.detail.request.xhr.statusText);
        this.dispatchEvent(new CustomEvent('toast', { bubbles: true, composed: true, detail: {
          msg: 'Error: ' + e.detail.request.xhr.status + " " + e.detail.request.xhr.statusText,
          duration: 5000
        } }));

        if (e.detail.request.xhr.responseURL.endsWith("api/v1/ownership")) {
          this.$.ownershipdialog._closeDialog();
        }
      },
      _makeList: function (items, property) {
        if (items && items.length)
          return items.map(function (item) {
            return item[property];
          });
      }
    });
  </script>
</dom-module>